#include "ObBullet.h"
#include "D3DApp.h"
#include "Collision.h"
#include "ObPlayer.h"
#include "ObDeathField.h"

ObBullet::ObBullet(IDirect3DTexture9* mtex, float px, float py, bool left){
	mID		= BL1;
	mInfo	= 0;
	mWidth	= 8.0f;
	mHeight	= 8.0f;
	mCenter = D3DXVECTOR3(mWidth/2, mHeight/2, 0.0f);
	mSpritePos.x = 8;
	mSpritePos.y = 8;

	mNeedCollision = true;
	mTex = mtex;
	mPos.x = px;
	mPos.y = py;
	
	mIsXGravity = false;
	mIsYGravity = true;

	mVelocity.x = 250.0f;
	mVelocity.y = 0.0f;

	if(left){
		mVelocity.x = - mVelocity.x;
	}
	
	MAX_VELOCITYX = 250.0f;
	MAX_VELOCITYY = 300.0f;

	mCurrentSprite = 0;
	count = 0.0f;
	mTime = 10.0f;
	flyCount = 0.0f;
}

ObBullet::~ObBullet(){

}

void ObBullet::collidedWith(ObObject* object, CollisionRS rs){
	switch (object->getId())
	{
	case ObPlayer::MARIO:
		
		break;
	case ObDeathField::Deathfield1:
		mNeedDispose = true;
		break;
	default:
		if(rs.mState == Left){
			mNeedDispose = true;
		}
		if(rs.mState == Right){
			mNeedDispose = true;
		}
		if(rs.mState == Bottom){
			mVelocity.y = 0.0f;
			mIsJump = false;
		}
		break;
	}
}
void ObBullet::update(float dt){
	if(mNeedDispose) return;
	mSpritePos.x = 8;
	count+=dt;
	if(count >= 0.3f){
		mCurrentSprite++;
		if(mCurrentSprite >3){
			mCurrentSprite = 0;
		}
		count = 0.0f;
		mSpritePos.x = 8.0f + 16*mCurrentSprite;
	}
	
	mTime -=dt;
	if(mTime<=0){
		mTime =0.0f;
		mNeedDispose = true;
	}

	if(mIsJump == false){
		mVelocity.y = 200.0f;
	}

}
void ObBullet::render(ID3DXSprite* mSprite){
	if(mNeedDispose) return;

	HR(gDevice->SetRenderState(D3DRS_ALPHATESTENABLE,true));
	D3DXMATRIX S;
	D3DXMatrixScaling(&S,0.6f,-0.6f,1.0f);
	D3DXMATRIX R;
	D3DXMatrixRotationZ(&R, mRotation);
	D3DXMATRIX T;
	D3DXMatrixTranslation(&T,mPos.x - gCamera->getPos().x, mPos.y - gCamera->getPos().y, 0);
	mSprite->SetTransform(&(S*R*T));
	RECT rect = calRenderRECT(mSpritePos,mWidth,mHeight);
	mSprite->Draw(mTex,&rect,&mCenter,0,D3DCOLOR_XRGB(255, 255, 255));
	mSprite->Flush();
	HR(gDevice->SetRenderState(D3DRS_ALPHATESTENABLE,false));
}
void ObBullet::clear(){
	mTex = NULL;
}

